import time

import datetime

import grabData

from MatchDirectory import BookUnit


quHost = 'http://m.qu.la'

def run(bookid=None):
    resList = BookUnit.find_all_no_content(bookid)
    print("需要下载数量>>({})".format(len(resList)))
    number = len(resList)
    i = 0
    for unit in resList:
        i+=1
        print("[{date}]正在下载第{index}条({progress}%) ->{title}".format(
                index=i,
                title=unit.title,
                progress=str(i/number*100)[0:4],
                date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        absurl = quHost+unit.url
        content =  grabData.grabContent(absurl)
        unit.update_to_db(absurl,content)
        print(">>>>>>设置内容为 url=>{} content=>{}".format(unit.absurl,unit.content[0:10]))#unit.content[0:40]))
        time.sleep(5)


if __name__ == '__main__':
    run()
